<template>
  <div>
    <commhead-2 name="购物车"/>
    <van-submit-bar :price="sumPrice*100" button-text="提交订单" @submit="onSubmit" v-show="list.length>0?true:false">
      <van-checkbox v-model="allcheck" >全选</van-checkbox>
    </van-submit-bar>
    <div class="contain" v-if="getToken()">
      <div v-if="list.length>0">
        <van-swipe-cell v-for="item in list" :key="item.goodId">
          <div class="item" >
            <span class="check">
              <van-checkbox v-model="item.selected" @change="changeselect(item.key,item.selected)" :disabled="checkflag"/>
            </span>
            <div class="item_card">
              <van-card
                :num="item.number"
                :price="item.price"
                :title="item.name"
                :thumb="item.pic"
              >
                <template #footer>
                  <van-stepper v-model="item.number" @change="changenum(item.key,item.number)"  :disabled="stepflag"/>
                </template>
              </van-card>
            </div>
          </div>
          <template #right>
            <van-button square text="删除" type="danger" class="delete-button" @click="dele(item.key)"/>
          </template>
        </van-swipe-cell>
      </div>
      <div v-else>
        <div class="back_groud">
          <div class="img_box">
            <img src="../../assets/img/cartpng.png" alt="" >
          </div>
          <span>
            暂无商品
          </span>
        </div>
      </div>
    </div>
    <div v-else>
      <div class="nogood">
        <span>登录后可同步账号购物车中的商品</span>
        <van-button type="danger" round @click="$router.push('/login')">登录</van-button>
      </div>
      <div class="back_groud">
        <div class="img_box">
          <img src="../../assets/img/cartpng.png" alt="" >
        </div>
        <span>
          登陆后同步购物车中的商品
        </span>
      </div>
    </div>
    <tabbar/>
  </div>
</template>

<script>
import { fetchCartInfo, setItemNum, itemStateChange, delItem } from '_api'
import { getToken } from '_utils'
import { Toast } from 'vant'
import Commhead2 from '../../components/Commhead2.vue'
import Tabbar from '../../components/Tabbar.vue'
export default {
  data () {
    return {
      list: [],
      sumPrice: 0,
      number: 0,

      // value: 1,
      stepflag: false,
      checkflag: false
    }
  },
  watch: {
    number: function (data) {
      this.$store.commit('car/set_cart_info', data)
    }
  },
  created () {
    if (getToken()) {
      this.fetchCartInfo()
    }
  },
  methods: {
    // 提交订单
    onSubmit () {
      // this.$router.push('/order')
      if (this.list.some(item => item.selected)) {
        this.$router.push('/order')
      } else {
        Toast('暂无选中商品')
      }
    },
    // 测试
    con () {
      console.log(this.$store)
    },
    // 删除商品
    dele (key) {
      // console.log(key)
      delItem({
        key,
        token: getToken()
      }).then(res => {
        console.log(res.data.code)
        if (res.data.code === 0) {
          this.fuzhi(res)
        } else {
          this.list = [],
          this.number = 0,
          this.sumPrice = 0
        }
      })
    },
    // 修改选中状态
    changeselect (key, selected) {
      this.checkflag = true
      itemStateChange({
        key,
        selected,
        token: getToken()
      }).then(res => {
        this.checkflag = false
        this.fuzhi(res)
      })
    },
    // 改变商品数控i昂
    changenum (key, num) {
      this.stepflag = true
      setItemNum({
        key,
        number: num,
        token: getToken()
      }).then(res => {
        this.fuzhi(res)
        this.stepflag = false
      })
    },
    // 获取购物车数据
    fetchCartInfo () {
      fetchCartInfo({
        token: getToken()
      }).then(res => {
        this.fuzhi(res)
      })
    },
    // 赋值
    fuzhi (res) {
      if (res.data.code === 0) {
        this.list = res.data.data.items
        this.sumPrice = res.data.data.price
        this.number = res.data.data.number
      } else {
        this.list = []
      }
    },
    getToken
  },
  computed: {
    allcheck: {
      get () {
        if (this.list.length) {
          return this.list.every(item => item.selected)
        } else {
          return false
        }
      },
      set (selected) {
        console.log(selected)
        const keys = []
        this.list.forEach(item => {
          keys.push(item.key)
        })
        itemStateChange({
          key: keys.join(','),
          selected,
          token: getToken()
        }).then(res => {
          this.fuzhi(res)
        })
        return selected
      }
    }
  },
  components: { Tabbar, Commhead2 }
}
</script>

<style lang="scss" scoped>
.delete-button {
    height: 100%;
}
.goods-card {
    margin: 0;
    background-color: white;
  }
.van-submit-bar{
  bottom: .5rem;
}
.contain{
  // height: 100vh;
  // background-color: #CCC;
  // padding: 0.4rem 0 1rem 0;
  padding-top: .5rem;
  padding-bottom: 1rem;
  .item{
    font-size: 16px;
    margin-bottom: 10px;
    background-color: white;
    display: flex;
    align-items: center;
    border-radius: 10px;
    overflow: hidden;
    .check{
      margin: 0 10px;
    }
    .item_card{
      flex: 1;
      .van-card{
        padding: 0;
        padding-right: 20px;
        text-align: left;
        padding-bottom: 10px;
        .van-card__content{
          justify-content: space-around;
          .van-multi-ellipsis--l2{
            flex: 1;
            font-size: 18px;
            height: 18px;
          }
          .van-card__price{
            color: red;
            font-size: 20px;
            .van-card__price-integer{
              font-size: 20px;
            }
          }
        }
      }
    }
  }
  /deep/.van-swipe-cell__right{
    top: -.3rem;
  }
}
.nogood{
  text-align: left;
  padding-top: 44px;
  font-size: 16px;
  line-height: 100%;
  background-color: white;
  display: flex;
  align-items: center;
  margin: 0 20px;
  justify-content: center;
  .van-button{
    height: 30px;
    padding: 0 10px;
  }
}
.back_groud{
  margin-top: 1rem;
  font-size: 16px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .img_box{
    width: .9rem;
    height: .9rem;
    img{
      width: 100%;
    }
  }
}
</style>
